import {Component, OnDestroy, OnInit} from '@angular/core';
import {ExtendHttpService} from "../../../common/extend-http.service";
import {TipService} from "../../services/tip.service";
import {Router} from "@angular/router";
import {SocketService} from "../../services/socket.service";
import {Subscription} from "rxjs";

@Component({
  selector: 'app-line-up-list',
  templateUrl: './line-up-list.component.html',
  styleUrls: ['./line-up-list.component.css']
})
export class LineUpListComponent implements OnInit, OnDestroy {
  tipType = 'success';
  isLoading = false;
  list = [];
  loading = true;
  isFlag = false;
  unMsMessage: Subscription;
  timeOut1 = null;
  timeOut2 = null;

  constructor(private extendHttp: ExtendHttpService,
              private tipService: TipService,
              private router: Router,
              private socketService: SocketService) {
  }

  ngOnInit() {
    cBack = () => {
      $('#btn-back').trigger('click');
    }
    this.unMsMessage = this.socketService.wsMessage.subscribe(data => {
      if (data.code == '1005') {
        setTimeout(() => {
          this.getData();
        }, 2000);
      }
    });
    this.getData();
  }

  getData() {
    this.extendHttp.post('/merchantApp/call/getList', {}).subscribe(response => {
      let data = response.json();
      if (data.code == '0') {
        this.list = data.list;
        this.isFlag = true;
        this.loading = false;
      } else if (data.code == '-2001') {
        this.tipService.change(true, '登录超时，跳转到登录页面');
        setTimeout(() => {
          this.router.navigate(['/login']);
        }, 2000);
      } else {
        this.tipType = 'error';
        this.loading = false;
        this.tipService.change(true, data.message);
      }
    }, error => {
      this.tipType = 'error';
      this.loading = false;
      this.tipService.change(true, '系统繁忙，操作失败！');
    });
  }

  // 叫号
  submitJH(id) {
    if (!this.isLoading) {
    this.isLoading = !this.isLoading;
    this.extendHttp.post('/merchantApp/call/yell', {id: id}).subscribe(response => {
      let data = response.json();
      if (data.code == '0') {
        this.tipType = 'success';
        this.isLoading = !this.isLoading;
        this.tipService.change(true, data.message);
      } else if (data.code == '-2001') {
        this.tipType = 'error';
        this.tipService.change(true, '登录超时，跳转到登录页面');
        setTimeout(() => {
          this.router.navigate(['/login']);
        }, 2000);
      } else {
        this.tipType = 'error';
        this.tipService.change(true, data.message);
        this.isLoading = !this.isLoading;
      }
    }, error => {
      this.tipType = 'error';
      this.tipService.change(true, '系统繁忙，操作失败！');
      this.isLoading = !this.isLoading;
    });
    }
  }

  // 入座
  submitRZ(id) {
    if (!this.isLoading) {
    this.isLoading = !this.isLoading;
    this.extendHttp.post('/merchantApp/call/seat', {id: id}).subscribe(response => {
      let data = response.json();
      if (data.code == '0') {
        this.tipType = 'success';
        this.isLoading = !this.isLoading;
        this.tipService.change(true, data.message);
        setTimeout(() => {
          this.timeOut1 = this.getData();
        }, 2000);
      } else if (data.code == '-2001') {
        this.tipType = 'error';
        this.tipService.change(true, '登录超时，跳转到登录页面');
        setTimeout(() => {
          this.router.navigate(['/login']);
        }, 2000);
      } else {
        this.tipType = 'error';
        this.tipService.change(true, data.message);
        this.isLoading = !this.isLoading;
      }
    }, error => {
      this.tipType = 'error';
      this.tipService.change(true, '系统繁忙，操作失败！');
      this.isLoading = !this.isLoading;
    });
    }
  }

  // 作废
  submitZF(id) {
    if (!this.isLoading) {
    this.isLoading = !this.isLoading;
    this.extendHttp.post('/merchantApp/call/cancel', {id: id}).subscribe(response => {
      let data = response.json();
      if (data.code == '0') {
        this.tipType = 'success';
        this.isLoading = !this.isLoading;
        this.tipService.change(true, data.message);
        setTimeout(() => {
          this.timeOut2 = this.getData();
        }, 2000);
      } else if (data.code == '-2001') {
        this.tipType = 'error';
        this.tipService.change(true, '登录超时，跳转到登录页面');
        setTimeout(() => {
          this.router.navigate(['/login']);
        }, 2000);
      } else {
        this.tipType = 'error';
        this.tipService.change(true, data.message);
        this.isLoading = !this.isLoading;
      }
    }, error => {
      this.tipType = 'error';
      this.tipService.change(true, '系统繁忙，操作失败！');
      this.isLoading = !this.isLoading;
    });
    }
  }

  ngOnDestroy() {
    this.unMsMessage.unsubscribe();
    if (this.timeOut1) {
      clearTimeout(this.timeOut1)
    }
    if (this.timeOut2) {
      clearTimeout(this.timeOut2)
    }
  }

}
